﻿package net.wangit.view.util;

import net.wangit.constants.WidthMode;
import net.wangit.util.ArrayUtil;
import net.wangit.util.Checker;
/**
 * 生成HTML的工具类
 * @author watson
 *
 */
public final class Html {

	private static final String ONE = "1";
	private static final String RELATIVE_STRING = "%";
	private static final String ABSOLUTE_STRING = "px";

	private Html() {
	}
	
	//获取度量单位
	private static String getWidthMode(int widthMode) {
		switch (widthMode) {
		case WidthMode.RELATIVE:
			return Html.RELATIVE_STRING;
		case WidthMode.ABSOLUTE:
			return Html.ABSOLUTE_STRING;
		}
		return Html.ABSOLUTE_STRING;
	}

	//追加任意字符串
	public static void set(StringBuffer html, String str) {
		if (!Checker.isEmpty(str))
			html.append(str);
	}

	//追加id属性值
	public static void setId(StringBuffer html, String id) {
		if (!Checker.isEmpty(id))
			html.append(" id=\"").append(id).append("\"");
	}

	//追加type属性值
	public static void setType(StringBuffer html, String type) {
		if (!Checker.isEmpty(type))
			html.append(" type=\"").append(type).append("\"");
	}
	
	//追加css样式
	public static void setCss(StringBuffer html, String css) {
		if (!Checker.isEmpty(css))
			html.append(" class=\"").append(css).append("\"");
	}

	//追加css样式，css为空时设为defaultCss样式
	public static void setCss(StringBuffer html, String css, String defaultCss) {
		if (!Checker.isEmpty(css))
			html.append(" class=\"").append(css).append("\"");
		else if (!Checker.isEmpty(defaultCss))
			html.append(" class=\"").append(defaultCss).append("\"");
	}

	//追加herf链接
	public static void setHerf(StringBuffer html, String herf) {
		if (!Checker.isEmpty(herf))
			html.append(" href=\"").append(herf).append("\"");
	}

	//追加title样式
	public static void setTitle(StringBuffer html, String title) {
		if (!Checker.isEmpty(title))
			html.append(" title=\"").append(title).append("\"");
	}
	
	//追加src源属性
	public static void setSrc(StringBuffer html, String src) {
		if (!Checker.isEmpty(src))
			html.append(" src=\"").append(src).append("\"");
	}

	//追加padding，对象四边的内边距
	public static void setPadding(StringBuffer html, int top, int left,
			int bottom, int right) {
		html.append(" style=\"");
		if (top >= 0)
			html.append("padding-top:").append(top).append("px;");
		if (left >= 0)
			html.append("padding-left:").append(left).append("px;");
		if (bottom >= 0)
			html.append("padding-bottom:").append(bottom).append("px;");
		if (right >= 0)
			html.append("padding-right:").append(right).append("px;");
		html.append("\"");
	}

	//追加span, 使用 span 属性可以指定 COLGROUP 定义的表格列数
	public static void setSpan(StringBuffer html, String span) {
		if (!Checker.isEmpty(span) && !span.equals(Html.ONE))
			html.append(" span=\"").append(span).append("\"");
	}

	//追加alt
	public static void setAlt(StringBuffer html, String alt) {
		if (!Checker.isEmpty(alt))
			html.append(" alt=\"").append(alt).append("\"");
	}

	//追加value值
	public static void setValue(StringBuffer html, String value) {
		html.append(" value=\"");
		if (value != null)
			html.append(value);
		html.append("\"");
	}

	//追加任意属性，attribute格式:name1="value1" name2="value2" ...
	public static void setAttribute(StringBuffer html, String attribute) {
		if (!Checker.isEmpty(attribute))
			html.append(" ").append(attribute);
	}

	//追加rows行数
	public static void setRows(StringBuffer html, int rows) {
		if (rows > 0)
			html.append(" rows=\"").append(rows).append("\"");
	}
	
	//追加cols列数
	public static void setCols(StringBuffer html, int cols) {
		if (cols > 0)
			html.append(" cols=\"").append(cols).append("\"");
	}

	//追加rows行数
	public static void setRows(StringBuffer html, String rows) {
		if (!Checker.isEmpty(rows))
			html.append(" rows=\"").append(rows).append("\"");
	}

	//追加cols列数
	public static void setCols(StringBuffer html, String cols) {
		if (!Checker.isEmpty(cols))
			html.append(" cols=\"").append(cols).append("\"");
	}

	//追加frameborder
	public static void setFrameBorder(StringBuffer html, String frameBorder) {
		if (!Checker.isEmpty(frameBorder))
			html.append(" frameborder=\"").append(frameBorder).append("\"");
	}

	//追加scrolling
	public static void setScrolling(StringBuffer html, String scrolling) {
		if (!Checker.isEmpty(scrolling))
			html.append(" scrolling=\"").append(scrolling).append("\"");
	}

	//追加rowspan占行数
	public static void setRowSpan(StringBuffer html, int rowSpan) {
		if (rowSpan > 1)
			html.append(" rowspan=\"").append(rowSpan).append("\"");
	}

	//追加colspan占列数
	public static void setColSpan(StringBuffer html, int colSpan) {
		if (colSpan > 1)
			html.append(" colspan=\"").append(colSpan).append("\"");
	}

	//追加width宽度
	public static void setWidth(StringBuffer html, int width, int mode) {
		if (width > 0)
			html.append(" width=\"").append(width).append(getWidthMode(mode))
					.append("\"");
	}

	//追加width宽度
	public static void setWidth(StringBuffer html, int width) {
		if (width > 0)
			html.append(" width=\"").append(width).append("\"");
	}

	//追加width宽度
	public static void setWidth(StringBuffer html, String width) {
		if (!Checker.isEmpty(width))
			html.append(" width=\"").append(width).append("\"");
	}

	//追加width样式
	public static void setStyleWithWidth(StringBuffer html, int width) {
		if (width > 0)
			html.append(" style=\"width:").append(width).append("px\"");
	}

	//追加width, height样式
	public static void setStyleWithWidthAndHeight(StringBuffer html, int width,
			int height) {
		if (width > 0 || height > 0) {
			html.append(" style=\"");
			if (width > 0)
				html.append("width:").append(width)
						.append(Html.ABSOLUTE_STRING);
			if (height > 0)
				html.append(";height:").append(height).append(
						Html.ABSOLUTE_STRING);
			html.append("\"");
		}
	}

	//追加width高度
	public static void setHeight(StringBuffer html, int height) {
		if (height > 0)
			html.append(" height=\"").append(height).append("\"");
	}

	//追加width高度
	public static void setHeight(StringBuffer html, int height, int mode) {
		if (height > 0)
			html.append(" height=\"").append(height).append(getWidthMode(mode))
					.append("\"");
	}

	//追加maxlength最大长度
	public static void setMaxLength(StringBuffer html, int maxLength) {
		if (maxLength > 0)
			html.append(" maxlength=\"").append(maxLength).append("\"");
	}

	//追加size
	public static void setSize(StringBuffer html, int size) {
		if (size > 0)
			html.append(" size=\"").append(size).append("\"");
	}

	//追加align横向对齐方式
	public static void setAlign(StringBuffer html, int align) {
		switch (align) {
		case 3:
			html.append(" align=\"right\"");
			break;

		case 2:
			html.append(" align=\"center\"");
			break;
		}
	}

	//追加valign坚直对齐方式
	public static void setVerticalAlign(StringBuffer html, int align) {
		switch (align) {
		case 1:
			html.append(" valign=\"top\"");
			break;

		case 3:
			html.append(" valign=\"bottom\"");
			break;
		}
	}

	//设置accesskey
	public static void setAccesskey(StringBuffer html, String accesskey) {
		if (!Checker.isEmpty(accesskey))
			html.append(" accesskey=\"").append(accesskey).append("\"");
	}

	//设置是否可见display样式
	public static void setVisible(StringBuffer html, boolean visible) {
		if (!visible)
			html.append(" style=\"display:none\"");
	}
	
	//设置checked是否选中
	public static void setChecked(StringBuffer html, String value,
			String itemValue) {
		if (value != null && value.equals(itemValue))
			html.append(" checked");
	}

	//设置checked是否选中
	public static void setChecked(StringBuffer html, String values[],
			String itemValue) {
		if (ArrayUtil.find(values, itemValue) >= 0)
			html.append(" checked");
	}

	//设置selected是否选中
	public static void setSelected(StringBuffer html, String value,
			String itemValue) {
		if (value != null && value.equals(itemValue))
			html.append(" selected");
	}

	//设置selected是否选中
	public static void setSelected(StringBuffer html, String values[],
			String itemValue) {
		if (ArrayUtil.find(values, itemValue) >= 0)
			html.append(" selected");
	}
}
